/*******************************************************************************
 * Copyright (c) 2023. 山东新北洋信息技术股份有限公司 保留所有权利。
 *
 ******************************************************************************/

/*
 * 版权所有 2009-2023山东新北洋信息技术股份有限公司 保留所有权利。
 */
package online.xuping.thousandsOfWorlds.demo.controller;

import cn.hutool.core.lang.Console;
import online.xuping.thousandsOfWorlds.common.event.OutEvent;
import online.xuping.thousandsOfWorlds.common.resp.CallResponse;
import online.xuping.thousandsOfWorlds.common.snowflake.SnowflakeIdWorker;
import online.xuping.thousandsOfWorlds.demo.domain.UserDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 【【请填写功能名称】Controller】
 *
 * @author cxz
 * @date 2023-04-10
 */
@Api(tags = "demo.项目实列")
@RestController
@RequestMapping("/user/warehouse")
public class UserController {


    @Autowired
    private ApplicationContext applicationContext;


    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 查询【请填写功能名称】列表
     *
     * @param user 入参
     * @return CallResponse
     * @author code-generator
     * @date 2023-04-10
     */
    @ApiOperation(value = "实列demo-查询列表数据", notes = "实列demo-查询列表数据")
    @PostMapping("/list")
    @CrossOrigin(origins = {"http://xuping.online", "https://xuping.online"})
    public CallResponse list(@RequestBody @Valid UserDomain user, @RequestAttribute("realName") String userName,
                             @RequestAttribute("roles") String[] roles,
                             @RequestHeader(value = "userId", defaultValue = "0") String userId,
                             BindingResult bindingResult
    ) {
        //方法1  需要在入参数中增加注解 @RequestAttribute("realName")   string
        //  @RequestAttribute("roles") String[]
        // @RequestHeader(value = "userId", defaultValue = "0")
        // 来获取对应的内容,按需加入到 controller 方法入参中即可
        Console.log("userId");
        Console.log(userId);
        Console.log("userName");
        Console.log(userName);
        Console.log("roles");
        Console.log(roles);


        //方法 2  独立使用不需要任何 引用和入参设置,直接调用静态工具类 代码如下


        if (bindingResult.hasErrors()) {
            return CallResponse.error("pageinfo");
        }
        OutEvent outEvent = new OutEvent(this);
        outEvent.setAssetCode("asdasd");
        outEvent.setWarehouseName("asdasd");
        applicationContext.publishEvent(outEvent);

        return CallResponse.success(Arrays.asList("asd", "asd", "asd"));

    }
}
